/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | foam-extend: Open Source CFD
   \\    /   O peration     | Version:     4.1
    \\  /    A nd           | Web:         http://www.foam-extend.org
     \\/     M anipulation  | For copyright notice see file Copyright
-------------------------------------------------------------------------------
License
	This file is part of foam-extend.

	foam-extend is free software: you can redistribute it and/or modify it
	under the terms of the GNU General Public License as published by the
	Free Software Foundation, either version 3 of the License, or (at your
	option) any later version.

	foam-extend is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

#include "DICSmoother.H"
#include "DICPreconditioner.H"

// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

namespace Foam
{
	defineTypeNameAndDebug(DICSmoother, 0);

	lduSmoother::addsymMatrixConstructorToTable<DICSmoother>
		addDICSmootherSymMatrixConstructorToTable_;
}


// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

Foam::DICSmoother::DICSmoother
(
	const lduMatrix& matrix,
	const FieldField<Field, scalar>& coupleBouCoeffs,
	const FieldField<Field, scalar>& coupleIntCoeffs,
	const lduInterfaceFieldPtrsList& interfaces
)
:
	lduSmoother
	(
		matrix,
		coupleBouCoeffs,
		coupleIntCoeffs,
		interfaces
	),
	rD_(matrix_.diag())
{
	DICPreconditioner::calcReciprocalD(rD_, matrix_);
}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

void Foam::DICSmoother::smooth
(
	scalarField& x,
	const scalarField& b,
	const direction cmpt,
	const label nSweeps
) const
{
	const scalar* const __restrict__ rDPtr = rD_.begin();
	const scalar* const __restrict__ upperPtr = matrix_.upper().begin();
	const label* const __restrict__ uPtr =
		matrix_.lduAddr().upperAddr().begin();

	const label* const __restrict__ lPtr =
		matrix_.lduAddr().lowerAddr().begin();

	// Temporary storage for the residual
	scalarField rA(rD_.size());
	scalar* __restrict__ rAPtr = rA.begin();

	for (label sweep=0; sweep<nSweeps; sweep++)
	{
		matrix_.residual
		(
			rA,
			x,
			b,
			coupleBouCoeffs_,
			interfaces_,
			cmpt
		);

		rA *= rD_;

		 label nFaces = matrix_.upper().size();
		for ( label face=0; face<nFaces; face++)
		{
			 label u = uPtr[face];
			rAPtr[u] -= rDPtr[u]*upperPtr[face]*rAPtr[lPtr[face]];
		}

		 label nFacesM1 = nFaces - 1;
		for ( label face=nFacesM1; face>=0; face--)
		{
			 label l = lPtr[face];
			rAPtr[l] -= rDPtr[l]*upperPtr[face]*rAPtr[uPtr[face]];
		}

		x += rA;
	}
}


// ************************************************************************* //
